package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.domain.SystemMenu;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    public void save(Role role, Long[] permissoinIds,Long[] menus) {
        roleMapper.insert(role);
        //维护角色与权限中间关系
        if (role.getPermissions() != null) {
            for (Permission permission : role.getPermissions()) {
                roleMapper.insertPermissionRoleRelation(role.getId(), permission.getId());
            }
        }
        //维护角色与菜单中间关系
        if (role.getMenus()!= null) {
            for (SystemMenu menu : role.getMenus()) {
                roleMapper.saveRolesystemMenuRalation(role.getId(),menu.getId());
            }
        }


  /*      if (permissoinIds != null) {
            for (Long roleId : permissoinIds) {
                roleMapper.saveRoleRoleRelation(role.getId(), roleId);
            }
        }*/
    }

    public void delete(Long id) {
        if (id != null) {
            //删除角色
            roleMapper.deleteByPrimaryKey(id);
            //删除角色与权限的关系
            roleMapper.deletePermissionRoleRelation(id);
            //删除角色与菜单的关系
            roleMapper.deleteRolesystemMenuRalationByRoleId(id);

        }
    }

    public void update(Role role, Long[] permissoinIds,Long[] menus) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deletePermissionRoleRelation(role.getId());
        roleMapper.deleteRolesystemMenuRalationByRoleId(role.getId());
        if (role.getPermissions() != null) {
            for (Permission permission : role.getPermissions()) {
                roleMapper.insertPermissionRoleRelation(role.getId(), permission.getId());
            }
        }

        //保存角色与菜单的关系
        if (role.getMenus()!= null) {
            for (SystemMenu menu : role.getMenus()) {
                roleMapper.saveRolesystemMenuRalation(role.getId(),menu.getId());
            }
        }

    }

    public Role get(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    public List<Role> getAll() {

        return roleMapper.selectAll();
    }

    public PageResult<Role> query(ObjectQuery qo) {
        int totalCount = roleMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<Role>(totalCount, Collections.<Role>emptyList());
        }
        return new PageResult<Role>(totalCount, roleMapper.queryForList(qo));
    }

    public List<Long> queryRoleIdsByEmpId(Long empId) {
        return roleMapper.queryRoleIdsByEmpId(empId);
    }

    public List<String> queryRoleSnByEmpId(Long id) {
        return roleMapper.queryRoleSnByEmpId(id);
    }
}
